package EGS.impl;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service570Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 出站检查 1、获取工位工序 2、查询条码本工序情况
	 */
	public EGS_execute_service570Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("570")) {// 整台车上，整台车下
				String iType = Mroot.element("body").element("operation").attribute("type").getValue();
				String iCartID = Mroot.element("body").element("operation").attribute("cartID").getValue();
//				String iCartSerial=Mroot.element("body").element("operation").attribute("cartSerial").getValue();
//				String iMachineTable=Mroot.element("body").element("operation").attribute("machineTable").getValue();
				String iTimestamp = Mroot.element("body").element("operation").attribute("timestamp").getValue();
				String iOperator = Mroot.element("body").element("operation").attribute("operator").getValue();

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				try {
					String strType = "卸载";
					if (iType.equals("0")) {
						String sqlstr = "delete from setupLocation where station = '" + iCartID + "'";
						sql.Command(sqlstr);
						sqlstr = "insert into setupLocation (station,cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity) "
								+ "(select cartID,cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity from setupLocation "
								+ "where cartID='" + iCartID + "' and station='" + iStation + "')";
						sql.Command(sqlstr);
						sqlstr = "delete from setupLocation where station = '" + iStation + "' and cartID='" + iCartID
								+ "'";
						sql.Command(sqlstr);

						sqlstr = "update materialID set location='cartID=" + iCartID + "' where materialID in "
								+ "(select reel from setupLocation " + "where cartID='" + iCartID + "') or preReel in "
								+ "(select reel from setupLocation where cartID='" + iCartID + "')";
						sql.Command(sqlstr);
					} else {
						String sqlstr = "delete from setupLocation where station = '" + iStation + "'";
						sql.Command(sqlstr);
						sqlstr = "insert into setupLocation (station,cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity) "
								+ "(select '" + iStation + "',cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity "
								+ "from setupLocation where cartID='" + iCartID + "' )";
						sql.Command(sqlstr);
						sqlstr = "delete from setupLocation where station != '" + iStation + "' and cartID='" + iCartID
								+ "'";
						sql.Command(sqlstr);

						sqlstr = "update materialID set location='" + iCartID + "@" + iStation
								+ "' where materialID in " + "(select reel from setupLocation " + "where cartID='"
								+ iCartID + "') or preReel in " + "(select reel from setupLocation where cartID='"
								+ iCartID + "')";
						sql.Command(sqlstr);
						strType = "安装";
					}
					String oOrder=getStationOrder(iStation);
					sql.Command(
							"insert into materialLog (state,operator,machineTime,materialID,quantity,location,feeder,tips,station,orderNumber) "
									+ "(select " + iType + ",'" + iOperator + "','" + iTimestamp
									+ "',reel,quantity,location,feeder,'cartID=" + iCartID + strType + "','"+iStation+"','"+oOrder+"' "
									+ "from setupLocation where cartID='" + iCartID + "')");

					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5701, "" + iStation + "_" + iCartID + "_" + iType);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5700, "" + iStation + "_" + iCartID + "_" + iType);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}
	
	private String getStationOrder(String iStation) throws Exception {
		if(iStation.endsWith("NPM-M2")) {// 应对M2不激活工单的特殊设定
			iStation=iStation.replaceAll("-M2", "-M1");
		}
		String str="select orderNumber from assetStation where ID='"+iStation+"'";
		JTable t=sql.select(str);
		String oOrder="";
		if(t!=null && t.getRowCount()>0) {
			oOrder=""+t.getValueAt(0, 0);
		}else {
			if(iStation.endsWith("M2")) {// 应对M2不激活工单的特殊设定
				iStation=iStation.replaceAll("-M2", "-M1");
			}
			str="select orderNumber from assetStation where ID='"+iStation+"'";
			t=sql.select(str);
			if(t!=null && t.getRowCount()>0) {
				oOrder=""+t.getValueAt(0, 0);
			}
		}
		return oOrder;
	}

}
